﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ServerAdapter.WcfGeebaService;

namespace WPFGeebaClient
{
    /// <summary>
    /// Interaction logic for threadControl.xaml
    /// </summary>
    public partial class threadControl : UserControl
    {
        private ServerAdapter.SAdapter server;
        private GThread myThread;
        private string subforumTopic;

        public threadControl()
        {
            InitializeComponent();
        }

        public threadControl(GThread thread,string subforumTopic)//,TreeViewItem parentTreeViewThreads)
        {
            InitializeComponent();
            this.tidTxt.Content = thread._id;
            this.threadTxt.Content = thread._subject;
            this.contentEditText.Text = thread._content;
            this.publishedByLabel.Content = thread._publisher.username;
            this.LastModifyLabel.Content = thread._modifiedDate;
            this.server = MainWindow.server;
            this.myThread = thread;
            this.subforumTopic = subforumTopic;
        }

        public void setCommentPosts(List<GThread> posts)
        {
            foreach (GThread post in posts)
            {
                postsListbox.Items.Add(new threadControl(post, this.subforumTopic));
                expanderPosts.Height += 200;
            }
        }

        private void expanderPosts_Expanded(object sender, RoutedEventArgs e)
        {
            if (postsListbox.Items.Count == 0)
            {
                List<GThread> posts = server.serverGetComments(MainWindow.user, this.subforumTopic, myThread._id).ToList<GThread>();
                setCommentPosts(posts);
            }
            if (postsListbox.Items.Count == 0)
            {
                this.postsListbox.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}
